# -*- coding: utf-8 -*- 

import datetime
import logging
import urllib2

from google.appengine.ext import db
from BeautifulSoup import BeautifulSoup

from model import *

def update_site_sold_total():
    # create group site
    group_site = get_site()

    # init group_info
    group_infos = list()

    # get groups
    today_groups = group_site.today_infos
    
    for group_info in today_groups:
        update_group_sold_total(group_info)
        

def update_group_sold_total(group_info):
    page = urllib2.urlopen(group_info.link)
    soup = BeautifulSoup(page)
    
    main_div = soup.findAll('div', attrs={'class':'today'})[0]
    sold_total = main_div.findAll('p', attrs={'class':'block18'})[0].findAll('span')[0].getText()[:-1]
        
    group_info.sold_total = int(sold_total)
    group_info.put()
    
    logging.info("groupinfo updated succeed : %s"%group_info.link)
        

def get_site():
    # create group site
    group_site = GroupSite.get_by_key_name("SITE_t55")
    if group_site is None:
        group_site = GroupSite(key_name="SITE_t55")
        group_site.code = "t55"
        group_site.name = "窝窝团".decode('utf8')
        group_site.link = db.Link("http://www.55tuan.com/index")
        group_site.pop = 60
        group_site.put()
    
    return group_site

def get_data():
    # create group site
    group_site = get_site()

    # init group_info
    group_infos = list()

    # get groups
    today_groups = group_site.today_infos

    if today_groups.count() == 0:
        
        # get site content
        page = urllib2.urlopen(group_site.link)
        
        # soup
        soup = BeautifulSoup(page)
        
        items = soup.findAll('div', attrs={'class' : 'today'})
        for item in items:
            title = item.findAll('div', attrs={'class':'biaoti'})[0].findAll('span')[1].getText()
            title = title.replace("\"", "")

            id = item.findAll('input', attrs={'name':'group_buy_id'})[0]['value']
            link = 'http://www.55tuan.com/tuan-%s'%(id)

            picture = item.findAll('div', attrs={'class':'pic'})[0].findAll('img')[0]['src']

            current_price = item.findAll('div', attrs={'class':'nub'})[0].getText().replace('&yen;','')

            table = item.findAll('table', attrs={'class':'deal-discount'})[0]
            tr = table.findAll('tr')[1]

            original_price = tr.findAll('td')[0].getText().replace('&yen;','')
            discount = tr.findAll('td')[1].getText()[:-1]

            sold_total = item.findAll('span', attrs={'id':'valid_goods'})[0].getText()[:-1]
                    

            # create group_info
            group_info = GroupInfo()
            group_info.site = group_site
            group_info.title = title
            group_info.current_price = float(current_price)
            group_info.original_price = float(original_price)
            group_info.discount = float(discount)
            group_info.picture = db.Link(picture)
            group_info.link = db.Link(link)
            group_info.sold_total = int(sold_total)
            group_info.create()
            
            group_infos.append(group_info)

            side_items = soup.findAll('div',attrs={'class':'gobuy_b'})[0].findAll('div', attrs={'class':'clearfix'})[0].findAll('div', attrs={'style':'border-bottom:#0499BA 1px solid; padding:10px 0 10px 0;'})
            for item in side_items:
                title = item.findAll('img')
                if len(title)==0 or title[0].has_key('alt') == False:
                    continue
                title = title[0]['alt']
                title = title.replace("\"", "")

                link = 'http://www.55tuan.com/%s'%(item.findAll('a')[0]['href'])

                picture = item.findAll('img')[0]['src']

                sold_total = item.findAll('span')[1].getText()

                # create group_info
                group_info = GroupInfo()
                group_info.site = group_site
                group_info.title = title
                #group_info.current_price = float(current_price)
                #group_info.original_price = float(original_price)
                #group_info.discount = float(discount)
                group_info.picture = db.Link(picture)
                group_info.link = db.Link(link)
                group_info.sold_total = int(sold_total)
                group_info.create()
                
                group_infos.append(group_info)

    else:
        for group in today_groups:
            group_infos.append(group)
        
    return group_infos


